﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;

namespace SABI
{
    public partial class frm0107 : Form
    {
        bool _isNew = true;
        string sql = "";
        string _WareHouseID;

        public frm0107()
        {
            InitializeComponent();
        }

        public frm0107(bool isNew = true, bool isView = false, string WareHouseID = "")
        {
            InitializeComponent();
            _isNew = isNew;
            btnSave.Visible = !isView;
            txtWareHouseID.ReadOnly = !isNew;
            btnAdd.Visible = isNew;
            _WareHouseID = WareHouseID;
        }

        private void frm0107_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm0107_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        void loadDATA()
        {
            if (!txtWareHouseID.ReadOnly) { return; }
            sql = string.Format("SELECT * FROM tb0055 WHERE WareHouseID = '{0}'", _WareHouseID);
            DataTable dt1 = DATA.GetDataTable(sql,FRM.ConnectString);
            if (dt1.Rows.Count == 0) { return; }
            txtWareHouseID.Text = _WareHouseID;
            txtWareHouseName.Text = dt1.Rows[0]["WareHouseName"].ToString();
            txtAddress.Text = dt1.Rows[0]["Address"].ToString();
            txtFullName.Text = dt1.Rows[0]["FullName"].ToString();
            chkIsTemp.Checked = dt1.Rows[0]["IsTemp"].ToString() == "1" ? true : false;
            chkDisabled.Checked = dt1.Rows[0]["Disabled"].ToString() == "1" ? true : false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            if (!_isNew)
            {
                UpdateInfo();
            }
            else
            {
                AddNew();
            }
        }

        private bool CheckInput()
        {
            if (txtWareHouseID.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Mã kho hàng!");
                txtWareHouseID.Focus();
                return false;
            }
            else if (txtWareHouseName.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Tên kho hàng!");
                txtWareHouseName.Focus();
                return false;
            }
            return true;
        }

        private void AddNew()
        {
            sql = "Insert Into tb0055( WareHouseID ,WareHouseName,DivisionID, ";
            sql += "Address,FullName, IsTemp, Disabled, ";
            sql += "CreateDate,CreateUserID,LastModifyDate,LastModifyUserID) VALUES(";
            sql += string.Format("'{0}',N'{1}','{2}',", txtWareHouseID.Text, txtWareHouseName.Text, FRM.DivisionID);
            sql += string.Format("N'{0}',N'{1}','{2}','{3}',", txtAddress.Text, txtFullName.Text, chkIsTemp.Checked?1:0, chkDisabled.Checked?1:0);
            sql += string.Format("GETDATE(),'{0}',GETDATE(),'{0}')", FRM.UserName);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Lưu dữ liệu thành công!");
                (Application.OpenForms["frm0099"] as frm0099).loadDATA_Grid1();
                btnSave.Enabled = false;
                txtWareHouseID.ReadOnly = true;
            }
            else
            {
                SDLL.DLL02.Alert("Lưu dữ liệu thất bại!");
            }
        }

        private void UpdateInfo()
        {
            sql = string.Format("UPDATE tb0055 SET WareHouseName =N'{0}', Address=N'{1}',FullName=N'{2}',", txtWareHouseName.Text, txtAddress.Text, txtFullName.Text);
            sql += string.Format("IsTemp='{0}',Disabled='{1}',", chkIsTemp.Checked ? 1 : 0, chkDisabled.Checked ? 1 : 0);
            sql += string.Format("LastModifyUserID='{0}', LastModifyDate= GETDATE() WHERE WareHouseID='{1}'", FRM.UserName, _WareHouseID);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frm0099"] as frm0099).loadDATA_Grid1();
                Close();
            }
            else
            {
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtWareHouseID.ReadOnly = false;
            txtWareHouseID.Text = "";
            txtWareHouseName.Text = "";
            txtAddress.Text = "";
            txtFullName.Text = "";
            btnSave.Enabled = true;
        }
    }
}
